﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.BusinessRule.InStoreDataMeasures;
using Jumpton.Persistent;
using Jumpton.SFA.Server.BusinessRule.CallRoute;

public partial class CallRoute_Visit_Record_Maitain : PageBase
{
    private OrgainzationManager orgManager = new OrgainzationManager();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            BindGrid();
            TxtStartDate.Text = txtEndDate.Text = DateTime.Now.ToString();
            BindddlVisitMode();
        }

    }

    private void BindGrid()
    {
        
        DataSet ds = CustomerRouteManager.SelectUserCustomer(int.Parse(MyStateManager.UserID.ToString()), this.MyStateManager.DomainID, this.MyStateManager.OrgID);
        if (ds != null && ds.Tables.Count > 0)
        {
            ddlCustomer.DataSource = ds.Tables[0];
            ddlCustomer.DataTextField = "NAME";
            ddlCustomer.DataValueField = "ID";
            ddlCustomer.DataBind();
            ddlCustomer.Items.Insert(0, new ListItem("---选择客户---",""));
        }
    }

    private void BindddlVisitMode()
    {
        ObjectTable ot = new VisitTypeManager().LoadTypes(MyStateManager.DomainID, true);
        ddlVisitMode.DataSource = ot;
        ddlVisitMode.DataTextField = "DESCRIPTION";
        ddlVisitMode.DataValueField = "ID";
        ddlVisitMode.DataBind();
        ddlVisitMode.Items.Insert(0, new ListItem("---所有---", ""));
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        VisitManager vrm = new VisitManager();
        VISITS visit = new VISITS();
        CUSTOMERS customer = new CustomerManager().Load(decimal.Parse(ddlCustomer.SelectedValue));
        
        visit.LAST_UPDATER = MyStateManager.Name;
        visit.CUSTOMERID = customer.ID;
        string aa = OrderHeaderManager.NewOrderId(visit.CUSTOMERID);
        visit.ID = decimal.Parse(aa);
        visit.STARTTIME = decimal.Parse(BusinessLogic.GetDateTimeString(DateTime.Parse(TxtStartDate.Text)));
        visit.STOPTIME = decimal.Parse(BusinessLogic.GetDateTimeString(DateTime.Parse(txtEndDate.Text)));
        visit.VISITTYPE = ddlVisitMode.SelectedValue;
        visit.USERID = MyStateManager.UserID;
        visit.DOMAINID = MyStateManager.DomainID;
        visit.STATUS = "2";
        //visit.LATITUDE = customer.IDENTITY2;
        //visit.LONGITUDE = customer.IDENTITY1;
        visit.OUTLINE = false;
        visit.orgid = MyStateManager.OrgID;
        visit.personid = MyStateManager.PersonID;
        visit.Note = txtVisitContent.Text.Trim();
        if (vrm.Save(visit))
        {
            ShowMessage("保存成功", Request.Url.ToString());
        }
        else
        {
            ShowMessage("保存失败");
        }
    }
}
